<?php
namespace MyExt;
use MyExt\MainController;
use MyExt\SaeAuth;
/*
*	用户控制器模板
*/

class UsersController extends MainController{

	public function __construct(){
		parent::__construct();
	}


	
	// 新浪登陆回调
	public function sinaback(){
		$sinaauth = new SaeAuth();
		if (isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$token = $sinaauth->getAccessToken( 'code', $keys ) ;
			if($token['uid']){
				$userName = 'sina_'.$token['uid'];
				$m = M('Tp_user');
				$uInfo = $m->where(array('user_account'=>$userName))->find();
				//用户不存在就创建
				if(!$uInfo){
					$data['user_ctime'] = time();	
					$data['user_account'] = $userName;	
					$data['user_active'] = 1;	
					$stat = $m->data($data)->add();
					if($stat > 0){
						$data['user_id'] = $stat;
						$uInfo = $data;
						$userInfo = $this->Main_Encrypt($uInfo);		//将用户信息加密后保存到SESSION
					}else{
						echo 'e2';		//用户添加失败
					}
				}
				$userInfo = $this->Main_Encrypt($uInfo);		//将用户信息加密后保存到SESSION
				session('userInfo',$userInfo);
				echo "<script>sessionStorage.userInfo = 'sina';</script>";
				echo "<script>location='http://shenghuo.th0435.top/fang.html'</script>";
			}
		}else{
			echo "登陆异常！";
		}
		
	}
	//http://shenghuo.th0435.top/index.php/User/User/sinaback

	//登陆
	public function login(){
		//新浪登陆
		$sinaauth = new SaeAuth();
		$url = $sinaauth->getAuthorizeURL();
		$this->assign('url',$url);
		
		$this->display();
	}


	//登陆处理
	public function login_c_ajax(){
		Token_V('login_c_ajax',I('post.token'));
		$userName = trim(I('post.userName'));
		$pwd = I('post.pwd');
		$vCode = trim(I('post.vCode'));
		if(!$userName || !$pwd || !$vCode){
			//$this->main_alertExit('登陆信息不得为空!');
			echo 'e1';exit;
		}
		$verify = new \Think\Verify();
		if(!$verify->check($vCode)){
			//$this->main_alertExit('验证码不正确！');
			echo 'e2';exit;
		}
		$m = M('Tp_user');
		$data = $m->where(array('user_account'=>$userName))->find();
		if($data['user_pwd'] != $this->main_encrypt($pwd)){
			//$this->main_alertExit('用户名或密码不正确！');
			echo 'e3';exit;
		}
		$userInfo = $this->Main_Encrypt($data);		//将用户信息加密后保存到SESSION
		session('userInfo',$userInfo);
		//$this->redirect(MODULE_NAME.'/Before/index');
		echo 'ok';
	}


	
	
	

	//退出 
	public function exits_ajax(){
		session('userInfo',null);
		if(!session(userInfo)){
			echo 'ok'; 
		}else{
			echo 'bad';
		}
	}


	//验证码
	public function vCode(){
		$this->Main_vCode();	//调用 ThinkPHP/Library/MyExt/Maincontroller/Main_vCode
	}


    //修改密码
    public function pwd_change_ajax(){
    	Token_V('pwd_change_ajax',I('post.token'));
        $m = M('Tp_user');
        $userInfo = $this->Main_Encrypt(session('userInfo'),1);
        if(!$this->Main_Empty_V(I('post.'))){
            echo 'e1';exit;	//信息填写不完整！
        }
        $yp = $m->where(array('user_id'=>$userInfo['user_id']))->field('user_pwd')->find();
        if($this->Main_Encrypt(trim(I('post.pwd')))!=$yp['user_pwd']){
            echo 'e2';exit;	//原密码不正确！
        }
        if(I('post.pwd_n') != I('post.pwd_n2')){
            echo 'e3';exit;	//两次新密码不一致！
        }
        if(I('post.pwd') == I('post.pwd_n')){
            echo 'e4';exit;	//新旧密码不得相同！
        }
        $pwd_n = $this->Main_Encrypt(I('post.pwd_n'));
        $sdata['user_id'] = $userInfo['user_id'];
        $sdata['user_pwd'] = $pwd_n;
        if($m->save($sdata)){
            echo 'ok';exit;	//密码修改成功！
        }else{
            echo 'e5';exit;	//密码修改失败！
        }
    }








	//注册
	public function register(){
		$this->display();
	}


	//注册处理
	public function register_c_ajax(){
		Token_V('register_c_ajax',I('post.token'));
		$userName = I('post.userName');
		$vCode = I('post.vCode');
		$module = I('post.module');
		if(!$userName || !$vCode){
			echo "e1";exit;	//信息不得为空!
		}
		$p = '/^\w+@\w{2,}\.[a-z]{2,3}$/';	
		if(!preg_match($p, $userName)){
			echo "e2";exit; 	//请正确输入邮箱!
		}
		$verify = new \Think\Verify();
		if(!$verify->check($vCode)){
			echo "e3";exit;		//验证码错误！
		}
		$m = M('Tp_user');
		$uInfo = $m->where(array('user_account'=>$userName))->find();
		if(time() < $uInfo['user_ctime']+10){
			echo 'e4';exit;		//信件发送太频繁，请休息一会儿！
		}
		if($uInfo && $uInfo['user_active'] != 0){
			echo 'e5';exit;		//该邮箱以被注册！
		}
		$uname = $this->main_encrypt($userName);
		$ucode = md5(rand());		//将邮箱加密，用于用户信件中地址的参数
		$urlcode['uname'] = $uname;
		$urlcode['ucode'] = $ucode;
		$urlcode['module'] = $module;
		$urlstat = base64_encode(json_encode($urlcode));
		$addr = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/User/User/register_back_ajax?urlstat='.$urlstat;
		$contents = " 尊敬的用户您好！请点击链接完成注册！链接有效时间15分钟！如点击无法跳转，请复制该地址在浏览器中打开！{$addr}";
		$mailStat = $this->main_sendEmail($userName,EMAILS,$contents);
		if(!$mailStat){
			echo 'e6';exit;		//系统异常，信件未发出！
		}
		$data['user_vcode'] = $ucode;
		$data['user_ctime'] = time();		//信件发送时间,用于记录和判断注册链接的有效期
		$data2['user_account'] = $userName;			//邮箱当做账户名
		if($uInfo){
			$stat = $m->data($data)->where($data2)->save();
		}else{
			$data3 = array_merge($data,$data2);
			$stat = $m->data($data3)->add();
		}
		if(!$stat){
			echo 'e7';exit;		//信件发出后数据异常，请联系管理员!
		}else{
			echo 'ok';exit;		//已向您的邮箱发送了一封确认信，请查看并确认
		}
	}



	//注册后设置密码
	public function register_back_ajax(){
		$data = json_decode(base64_decode(I('urlstat')),1);
		//dump($data);
		$mail = Encrypt($data['uname'],1);
		$code = $data['ucode'];
		$module = $data['module'];
		session('current_module',$module);
		$m = M('Tp_user');
		$uInfo = $m->where(array('user_account'=>$mail))->find();
		if(!$uInfo){
			echo "<h1 style='line-height:100px;color:red;text-align:center'>出现异常，请重新注册！</h1>";exit;
		}
		if($uInfo['user_ctime']+900 < time()){
			echo "<h1 style='line-height:100px;color:red;text-align:center'>注册链接已过期，请重新注册！</h1>";exit;
		}
		if($uInfo['user_vcode'] != $code){
			echo "<h1 style='line-height:100px;color:red;text-align:center'>注册链接已失效，请重新注册！</h1>";exit;
		}
		session('register_mail',$mail);
	//	$this->redirect($module.'/Before/index');
		$this->redirect('register_back_set');
	}



	//注册设置密码的处理
	public function register_back_c_ajax(){
		Token_V('register_back_c_ajax',I('post.token'));

		session('register_mail',null);
		$email = I('post.email');
		$pwd  = I('post.pwd');
		if(!$email || !$pwd){
			echo 'e1';exit;		//信息添写不完整！
		}
		$m = M('Tp_user');
		$uInfo = $m->where(array('user_account'=>$email))->find();
		$utime = $uInfo['user_ctime'];
		if(!$uInfo){
			echo 'e2';exit;		//出现异常，请重新注册！
		}
		$data['user_pwd'] = $this->Main_Encrypt($pwd);
		$data['user_vcode'] = md5(microtime(1));
		$data['user_active'] = 1;
		$stat = $m->data($data)->where(array('user_account'=>$email))->save();
		if($stat){
			echo 'ok';exit;		//注册成功！
		}else{
			echo 'e4';exit;		//注册异常！
		}
	}





	//密码找回
	public function pwd_find(){
		$this->display();
	}



	//密码找回处理
	public function pwdfind_c_ajax(){
		Token_V('pwdfind_c_ajax',I('post.token'));
		$userName = I('post.userName');
		$vCode = I('post.vCode');
		$module = I('post.module');
		if(!$userName || !$vCode){
			echo "e1";exit;	//信息不得为空!
		}
		$p = '/^\w+@\w{2,}\.[a-z]{2,3}$/';	
		if(!preg_match($p, $userName)){
			echo "e2";exit; 	//请正确输入邮箱!
		}
		$verify = new \Think\Verify();
		if(!$verify->check($vCode)){
			echo "e3";exit;		//验证码错误！
		}
		$m = M('Tp_user');
		$uInfo = $m->where(array('user_account'=>$userName))->find();
		if(time() < $uInfo['user_ctime']+10){
			echo 'e4';exit;		//信件发送太频繁，请休息一会儿！
		}
		if($uInfo && $uInfo['user_active'] == 0){
			echo 'e5';exit;		//该邮箱以被注册！
		}
		$uname = $this->main_encrypt($userName);
		$ucode = md5(rand());		//将邮箱加密，用于用户信件中地址的参数
		$urlcode['uname'] = $uname;
		$urlcode['ucode'] = $ucode;
		$urlcode['module'] = $module;
		$urlstat = base64_encode(json_encode($urlcode));
		$addr = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/User/User/pwdfind_back_ajax?urlstat='.$urlstat;
		$contents = " 尊敬的用户您好！您正在找回密码！链接有效时间15分钟！如点击无法跳转，请复制该地址在浏览器中打开！{$addr}";
		$mailStat = $this->main_sendEmail($userName,EMAILS,$contents);
		if(!$mailStat){
			echo 'e6';exit;		//系统异常，信件未发出！
		}
		$data['user_vcode'] = $ucode;
		$data['user_ftime'] = time();		//信件发送时间,用于记录和判断注册链接的有效期
		$data2['user_account'] = $userName;			//邮箱当做账户名
		$stat = $m->data($data)->where($data2)->save();
		if(!$stat){
			echo 'e7';exit;		//信件发出后数据异常，请联系管理员!
		}else{
			echo 'ok';exit;		//已向您的邮箱发送了一封确认信，请查看并确认
		}
	}



	//密码找回后设置密码
	public function pwdfind_back_ajax(){
		$data = json_decode(base64_decode(I('urlstat')),1);
		$mail = $this->main_encrypt($data['uname'],1);
		$code = $data['ucode'];
		$module = $data['module'];
		session('current_module',$module);
		session('pwdfind_mail',$mail);
	//	dump($data);
		$m = M('Tp_user');
		$uInfo = $m->where(array('user_account'=>$mail))->find();
		if(!$uInfo){
			echo "<h1 style='line-height:100px;color:red;text-align:center'>出现异常，请重新注册！</h1>";exit;
		}
		if($uInfo['user_ftime']+900 < time()){
			echo "<h1 style='line-height:100px;color:red;text-align:center'>注册链接已过期！</h1>";exit;
		}
		if($uInfo['user_vcode'] != $code){
			echo "<h1 style='line-height:100px;color:red;text-align:center'>注册链接已失效！</h1>";exit;
		}
		//$this->redirect($module.'/Before/index');
		$this->redirect('pwdfind_back_set');
	}



	//密码找回设置密码的处理
	public function pwdfind_back_c_ajax(){
		Token_V('pwdfind_back_c_ajax',I('post.token'));
		session('pwdfind_mail',null);
		$email = I('post.email');
		$pwd  = I('post.pwd');
		if(!$email || !$pwd){
			echo 'e1';exit;		//信息添写不完整！
		}
		$m = M('Tp_user');
		$uInfo = $m->where(array('user_account'=>$email))->find();
		if(!$uInfo){
			echo 'e2';exit;		//出现异常，请重新注册！
		}
		$data['user_pwd'] = $this->Main_Encrypt($pwd);
		$data['user_vcode'] = md5(microtime(1));
		$stat = $m->data($data)->where(array('user_account'=>$email))->save();
		if($stat){
			echo 'ok';exit;		//注册成功！
		}else{
			echo 'e4';exit;		//注册异常！
		}
	}






}